package interfaze;

import java.awt.Image;
import java.awt.Polygon;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import logic.Mp3Player;
import logic.SelectGameThread;
import logic.Utilities;

/**The Game Selector Panel's class.
 * @author Team 1
 *
 */
@SuppressWarnings("serial")
public class GameSelectorPanel extends JPanel implements MouseListener, MouseMotionListener, KeyListener, FocusListener {
	
	private SuperArcade2in1 window;							//The JFrame in which this panel is being added.
	private JLabel brickBreakerOnLabel = new JLabel();		//The brick breaker's label.
	private JLabel spaceInvadersOnLabel = new JLabel();		//The space invader's label.
	private JLabel title = new JLabel();					//The title label.
	private JLabel leftArrow = new JLabel();				//The left arrow label.
	private JLabel rightArrow = new JLabel();				//The right arrow label.
	private Polygon brickBounds = new Polygon();			//The boundaries of the brick label.
	private Polygon spaceBounds = new Polygon();			//The boundaries of the space label.
	private ImageIcon iconBrickBreakerOn;					//ImageIcon to be loaded.
	private ImageIcon iconSpaceInvadersOn;					//ImageIcon to be loaded.
	private ImageIcon iconBrickBreakerOff;					//ImageIcon to be loaded.
	private ImageIcon iconSpaceInvadersOff;					//ImageIcon to be loaded.
	private ImageIcon iconTitleOn;							//ImageIcon to be loaded.
	private ImageIcon iconTitleOff;							//ImageIcon to be loaded.
	private ImageIcon iconLeftArrowOn;						//ImageIcon to be loaded.
	private ImageIcon iconLeftArrowOff;						//ImageIcon to be loaded.
	private ImageIcon iconRightArrowOn;						//ImageIcon to be loaded.
	private ImageIcon iconRightArrowOff;					//ImageIcon to be loaded.
	private boolean isOnBrick = false;						//The flag to know if the mouse is on brick's boundaries.
	private boolean isOnSpace = false;						//The flag to know if the mouse is on space's boundaries.
	private boolean isTitleOn = true;						//The flag to know if the title is activated.
	private SelectGameThread titleChange;					//The thread that gives to the title the lightning effect.
	
	/**GameSelectorPanel's constructor.
	 * @param window : The JFrame in which this panel is being added.
	 */
	public GameSelectorPanel(SuperArcade2in1 window) {
		this.window = window;
		
		//Sets panel's layout to null in order to work with absolute coordinates.
		this.setLayout(null);
		
		//If the music is stopped.
		if(window.player == null) {
			//Starts playing the music.
			window.player = new Mp3Player("res/Lastation.mp3");
			window.player.start();
		}
		
		//Sets the images.
		Image titleOn = Utilities.loadImage("res/SelectGameOn.png");
		Image titleOff = Utilities.loadImage("res/SelectGameOff.png");
		Image leftArrowOn = Utilities.loadImage("res/ArrowLeftOn.png");
		Image leftArrowOff = Utilities.loadImage("res/ArrowLeftOff.png");
		Image rightArrowOn = Utilities.loadImage("res/ArrowRightOn.png");
		Image rightArrowOff = Utilities.loadImage("res/ArrowRightOff.png");
		Image brickBreakerOn = Utilities.loadImage("res/BrickBreakerOn.png");
		Image brickBreakerOff = Utilities.loadImage("res/BrickBreakerOff.png");
		Image spaceInvadersOn = Utilities.loadImage("res/EarthDefenderOn.png");
		Image spaceInvadersOff = Utilities.loadImage("res/EarthDefenderOff.png");
		if(window.xScale != 1 || window.yScale != 1) {
			titleOn = Utilities.resizeImage(window.xScale, window.yScale, "res/SelectGameOn.png");
			titleOff = Utilities.resizeImage(window.xScale, window.yScale, "res/SelectGameOff.png");
			leftArrowOn = Utilities.resizeImage(window.xScale, window.yScale, "res/ArrowLeftOn.png");
			leftArrowOff = Utilities.resizeImage(window.xScale, window.yScale, "res/ArrowLeftOff.png");
			rightArrowOn = Utilities.resizeImage(window.xScale, window.yScale, "res/ArrowRightOn.png");
			rightArrowOff = Utilities.resizeImage(window.xScale, window.yScale, "res/ArrowRightOff.png");
			brickBreakerOn = Utilities.resizeImage(window.xScale, window.yScale, "res/BrickBreakerOn.png");
			brickBreakerOff = Utilities.resizeImage(window.xScale, window.yScale, "res/BrickBreakerOff.png");
			spaceInvadersOn = Utilities.resizeImage(window.xScale, window.yScale, "res/EarthDefenderOn.png");
			spaceInvadersOff = Utilities.resizeImage(window.xScale, window.yScale, "res/EarthDefenderOff.png");
		}
		
		//Sets the components icons, boundaries and so on.
		title.setBounds(Math.round((window.width/2) - (titleOn.getWidth(null) / 2)), Math.round(-50 * window.yScale),
				titleOn.getWidth(null), titleOn.getHeight(null));
		iconTitleOn = new ImageIcon(titleOn);
		iconTitleOff = new ImageIcon(titleOff);
		title.setIcon(iconTitleOn);
		
		iconLeftArrowOn = new ImageIcon(leftArrowOn);
		iconLeftArrowOff = new ImageIcon(leftArrowOff);
		leftArrow.setBounds(Math.round(window.width/3 + (50 * window.xScale)), Math.round((window.height/2) - (leftArrowOff.getHeight(null) / 2 * window.yScale)),
				leftArrowOff.getWidth(null), leftArrowOff.getHeight(null));
		leftArrow.setIcon(iconLeftArrowOff);
		
		iconRightArrowOn = new ImageIcon(rightArrowOn);
		iconRightArrowOff = new ImageIcon(rightArrowOff);
		rightArrow.setBounds(Math.round(window.width/2), Math.round((window.height/2) - (rightArrowOff.getHeight(null) / 2 * window.yScale)),
				rightArrowOff.getWidth(null), rightArrowOff.getHeight(null));
		rightArrow.setIcon(iconRightArrowOff);
		
		iconBrickBreakerOff = new ImageIcon(brickBreakerOff);
		iconBrickBreakerOn = new ImageIcon(brickBreakerOn);
		brickBreakerOnLabel.setBounds(0, 0, brickBreakerOn.getWidth(null), brickBreakerOn.getHeight(null));
		brickBreakerOnLabel.setIcon(iconBrickBreakerOff);
		
		brickBounds.addPoint(0, 0);
		brickBounds.addPoint(Math.round(1004 * window.xScale), 0);
		brickBounds.addPoint(Math.round(224 * window.xScale), window.height);
		brickBounds.addPoint(0, window.height);
		
		iconSpaceInvadersOff = new ImageIcon(spaceInvadersOff);
		iconSpaceInvadersOn = new ImageIcon(spaceInvadersOn);
		spaceInvadersOnLabel.setBounds(0, 0, spaceInvadersOn.getWidth(null), spaceInvadersOn.getHeight(null));
		spaceInvadersOnLabel.setIcon(iconSpaceInvadersOff);
		
		spaceBounds.addPoint(Math.round(1005 * window.xScale), 0);
		spaceBounds.addPoint(window.width, 0);
		spaceBounds.addPoint(window.width, window.height);
		spaceBounds.addPoint(Math.round(225 * window.xScale), window.height);
		
		//Add the components to the panel.
		this.add(title);
		this.add(rightArrow);
		this.add(leftArrow);
		this.add(brickBreakerOnLabel);
		this.add(spaceInvadersOnLabel);
		
		//Adds the listeners to the components.
		this.addMouseListener(this);
		this.addMouseMotionListener(this);
		this.addKeyListener(this);
		this.addFocusListener(this);
		
		//Starts the thread that gives the lightning effect to the title.
		titleChange = new SelectGameThread(this);
		titleChange.start();
		
		//Set focusable and request the focus.
		this.setFocusable(true);
		this.requestFocus();
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if(brickBounds.contains(e.getX(), e.getY())) {
			//Stops the music.
			window.player.stopPlayer();
			window.player = null;
			//Redirects to brick breaker's main menu.
			window.add(new BrickBreakerMainMenuPanel(window));
			this.setVisible(false);
			titleChange.interrupt();
			window.remove(this);
		} else if(spaceBounds.contains(e.getX(), e.getY())) {
			//Stops the music.
			window.player.stopPlayer();
			window.player = null;
			//Redirects to brick breaker's main menu.
			window.add(new SpaceInvadersMainMenuPanel(window));
			this.setVisible(false);
			titleChange.interrupt();
			window.remove(this);
		}
	}
	@Override
	public void mouseMoved(MouseEvent e) {
		if(brickBounds.contains(e.getX(), e.getY())) {
			if(isOnSpace == true) {
				//Turns off the earth defender's arrow and panel.
				isOnSpace = false;
				rightArrow.setIcon(iconRightArrowOff);
				this.repaint();
				spaceInvadersOnLabel.setIcon(iconSpaceInvadersOff);
			}
			//Turns on the brick breaker's arrow and panel.
			isOnBrick = true;
			brickBreakerOnLabel.setIcon(iconBrickBreakerOn);
			this.repaint();
			leftArrow.setIcon(iconLeftArrowOn);
		} if(spaceBounds.contains(e.getX(), e.getY())) {
			if(isOnBrick) {
				//Turns off the brick breaker's arrow and panel.
				isOnBrick = false;
				leftArrow.setIcon(iconLeftArrowOff);
				this.repaint();
				brickBreakerOnLabel.setIcon(iconBrickBreakerOff);
			}
			//Turns on the earth defender's arrow and panel.
			isOnSpace = true;
			spaceInvadersOnLabel.setIcon(iconSpaceInvadersOn);
			this.repaint();
			rightArrow.setIcon(iconRightArrowOn);
		}
	}
	
	@Override
	public void mouseExited(MouseEvent e) {
		//If mouse exits from the window.
		if(e.getSource().equals(this)) {
			if(isOnBrick) {
				//Turns off the brick breaker's arrow and panel.
				brickBreakerOnLabel.setIcon(iconBrickBreakerOff);
				leftArrow.setIcon(iconLeftArrowOff);
				isOnBrick = false;
			} else if(isOnSpace) {
				//Turns off the earth defender's arrow and panel.
				spaceInvadersOnLabel.setIcon(iconSpaceInvadersOff);
				rightArrow.setIcon(iconRightArrowOff);
				isOnSpace = false;
			}
		}
	}
	
	/**Changes the image of the title if it's on it set to off and the other way around.
	 */
	public void changeTitle() {
		if (isTitleOn) {
			title.setIcon(iconTitleOff);
			isTitleOn = false;
		} else {
			title.setIcon(iconTitleOn);
			isTitleOn = true;
		}
	}

	@Override
	public void keyReleased(KeyEvent e) {
		//If ESC is pressed redirects to global main menu.
		if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
			window.add(new GlobalMenuPanel(window));
			this.setVisible(false);
			window.remove(this);
		}
	}
	@Override
	public void focusGained(FocusEvent e) {

	}

	@Override
	public void focusLost(FocusEvent e) {
		this.requestFocus();
	}

	@Override
	public void keyTyped(KeyEvent e) {
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		
	}
	@Override
	public void mousePressed(MouseEvent e) {
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		
	}
	
	@Override
	public void mouseEntered(MouseEvent e) {
		
	}
	
	@Override
	public void mouseDragged(MouseEvent e) {
		
	}
}